const fs = require('fs')
const path = require('path')
const axios = require('axios')
const dayjs = require('dayjs')
const superagent = require('superagent')
const cheerio = require('cheerio')
const xlsx = require('node-xlsx').default
const menus = fs.readdirSync(path.join(__dirname, 'datas'), 'utf-8')
process.on('unhandledRejection', (reason, promise) => {
    console.log('Unhandled Rejection:', reason)
    // 在这里处理
})
var books = []
for (let i = 0; i < menus.length; i++) {
    const file = fs.readFileSync(path.join(__dirname, `datas/${menus[i]}`), 'utf-8')
    books = books.concat(JSON.parse(file))
}
start()
// 完善资料
async function start() {
    for (let i = 0; i < books.length; i++) {
        console.log(`总共有${books.length}本`)
        console.log(`正在执行第${i + 1}本`)
        console.log(`isbn:${books[i].isbn}`)
        let names = books[i].author.name.filter(function (s) {
            return s && s.trim(); // 注：IE9(不包含IE9)以下的版本没有trim()方法
        });
        if (names.length <= 0) {
            const author = await getAuthor(books[i].isbn).catch((error) => {
                console.log(`getAuthor error: ${error}`)
            })
            books[i].author = author
        }
        // 图片空或者为豆瓣的默认图
        if (books[i].images.trim() === '' || books[i].images === 'https://img1.doubanio.com/f/shire/5522dd1f5b742d1e1394a17f44d590646b63871d/pics/book-default-lpic.gif') {
            const cover = await getBookCover(books[i].isbn).catch((error) => {
                console.log(`getBookCover error: ${error}`)
            })
            books[i].images = cover
        }
        if (books[i].summary.trim() === '') {
            const summary = await getBookSummary(books[i].isbn).catch((error) => {
                console.log(`getBookSummary error: ${error}`)
            })
            books[i].summary = summary
        }
        if (books[i].price.trim() === '') {
            const price = await getBookPirce(books[i].isbn).catch((error) => {
                console.log(`getBookPirce error: ${error}`)
            })
            books[i].price = price
        }
        if (books[i].pubdate.trim() === '') {
            const pubdate = await getBookPushDate(books[i].isbn).catch((error) => {
                console.log(`getBookPushDate error: ${error}`)
            })
            books[i].pubdate = pubdate
        }
        if (books[i].publisher.trim() === '') {
            const publisher = await getBookPublisher(books[i].isbn).catch((error) => {
                console.log(`getBookPublisher error: ${error}`)
            })
            books[i].publisher = publisher
        }

    }
    // exportExcel()
    exportJson()
}

// 获取书本封面信息
function getBookCover(isbn) {
    return new Promise(async (resolve) => {
        // 默认为豆瓣的空图片
        let cover = 'https://img1.doubanio.com/f/shire/5522dd1f5b742d1e1394a17f44d590646b63871d/pics/book-default-lpic.gif'
        let res
        let url
        // 孔夫子旧书网
        url = `http://search.kongfz.com/product_result/?key=${isbn}&status=0&_stpmt=eyJzZWFyY2hfdHlwZSI6ImFjdGl2ZSJ9&pagenum=1&ajaxdata=1&type=1&_=${dayjs().valueOf()}`
        res = await axios.get(url).catch(() => {})
        if (res.data && res.data.data && res.data.data.itemList && res.data.data.itemList.length > 0) {
            const book = res.data.data.itemList[0]
            if (book.bigimgurl && book.bigimgurl !== '') {
                cover = book.bigimgurl
                resolve(cover)
                return
            }
        }

        // 缺书网
        // http://www.queshu.com/search/?c=9787502646967
        res = await superagent.get(`http://www.queshu.com/search/?c=${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#jianlou_book').length > 0) {
                url = $('#jianlou_book .book_right_line a').eq(0).attr('href')
                res = await superagent.get(`http://www.queshu.com${url}`).catch(() => {})
                if (res && res.text) {
                    $ = cheerio.load(res.text);
                    if ($('#book_pic #pic200 img').length > 0) {
                        image = $('#book_pic #pic200 img').attr('src')
                        if (image && image !== '') {
                            cover = image
                            resolve(cover)
                            return
                        }
                    }
                }
            }
        }

        resolve(cover)
        return
    })
}

// 获取书本简介
function getBookSummary(isbn) {
    return new Promise(async (resolve) => {
        let summary = ''
        let res
        let url
        let $
        // 孔夫子旧书网
        res = await axios.get(`http://search.kongfz.com/product_result/?key=${isbn}&status=0&_stpmt=eyJzZWFyY2hfdHlwZSI6ImFjdGl2ZSJ9&pagenum=1&ajaxdata=1&type=1&_=${dayjs().valueOf()}`).catch(() => {
            resolve(summary)
        })
        if (res.data && res.data.data && res.data.data.itemList && res.data.data.itemList.length > 0) {
            const book = res.data.data.itemList[0]
            const url = `http://book.kongfz.com/${book.shopid}/${book.itemid}`
            const html = await superagent.get(url).catch(() => {})
            if (html && html.text) {
                let $ = cheerio.load(html.text);
                if ($('.brief-intro-text').length > 0) {
                    summary = $('.brief-intro-text dl').eq(0).find('p').text()
                    resolve(summary)
                    return
                }
            }
        }

        // 书号查询官网 http://www.shuhaochaxun.com/isbn/9787302524595
        res = await superagent.get(`http://www.shuhaochaxun.com/isbn/${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#zw .fengmian').length > 0) {
                summary = $('#zw p').text()
                resolve(summary)
                return
            }
        }

        // 缺书网
        // http://www.queshu.com/search/?c=9787502646967
        res = await superagent.get(`http://www.queshu.com/search/?c=${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#jianlou_book').length > 0) {
                url = $('#jianlou_book .book_right_line a').eq(0).attr('href')
                res = await superagent.get(`http://www.queshu.com${url}`).catch(() => {})
                if (res && res.text) {
                    $ = cheerio.load(res.text);
                    if ($('#detail_intro .detail_body').length > 0) {
                        summary = $('#detail_intro .detail_body').text()
                        if (summary && summary !== '') {
                            resolve(summary)
                            return
                        }
                    }
                }
            }
        }

        resolve(summary)
        return
    })
}


// 获取书籍出版时间
function getBookPushDate(isbn) {
    return new Promise(async (resolve) => {
        debugger
        let date = ''
        let res
        let $
        // 书号查询官网 http://www.shuhaochaxun.com/isbn/9787302524595
        res = await superagent.get(`http://www.shuhaochaxun.com/isbn/${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#zw .fengmian').length > 0) {
                date = $('#zw .fll ul li').eq(6).text().replace('出版时间：', '')
                if (date && date !== '') {
                    date = dayjs(date).format('YYYY-MM')
                    resolve(date)
                    return
                }
            }
        }

        resolve(date)
        return
    })
}

// 获取书籍出版社
function getBookPublisher(isbn) {
    return new Promise(async (resolve) => {
        let publisher = ''
        let res
        let $
        let url
        // 书号查询官网 http://www.shuhaochaxun.com/isbn/9787302524595
        res = await superagent.get(`http://www.shuhaochaxun.com/isbn/${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#zw .fengmian').length > 0) {
                publisher = $('#zw .fll ul li').eq(4).text().replace('出版社', '')
                if (publisher && publisher !== '') {
                    resolve(publisher)
                    return
                }
            }
        }

        // 缺书网
        // http://www.queshu.com/search/?c=9787502646967
        res = await superagent.get(`http://www.queshu.com/search/?c=${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#jianlou_book').length > 0) {
                url = $('#jianlou_book .book_right_line a').eq(0).attr('href')
                res = await superagent.get(`http://www.queshu.com${url}`).catch(() => {})
                if (res && res.text) {
                    $ = cheerio.load(res.text);
                    if ($('.book_info a').length > 0) {
                        publisher = $('.book_info a').eq(0).text()
                        if (publisher && publisher !== '') {
                            resolve(publisher)
                            return
                        }
                    }
                }
            }
        }

        resolve(publisher)
        return
    })
}

// 获取书籍价格
function getBookPirce(isbn) {
    return new Promise(async (resolve) => {
        let price = ''
        let res
        let $
        // 书号查询官网 http://www.shuhaochaxun.com/isbn/9787302524595
        res = await superagent.get(`http://www.shuhaochaxun.com/isbn/${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#zw .fengmian').length > 0) {
                price = $('#zw .fll ul li').eq(7).text().replace('出版价格', '')
                if (price && price !== '') {
                    resolve(price)
                    return
                }
            }
        }

        // 缺书网
        // http://www.queshu.com/search/?c=9787502646967
        res = await superagent.get(`http://www.queshu.com/search/?c=${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#jianlou_book').length > 0) {
                let _price = $('#jianlou_book .book_right_line .left .dingjia').text()
                _price = _price.replace('定　价：', '')
                price = _price
                if (price && price !== '') {
                    resolve(price)
                    return
                }
            }
        }


        resolve(price)
        return
    })
}

// 获取作者信息
function getAuthor(isbn) {
    return new Promise(async (resolve) => {
        const author = {
            name: [],
            intro: ''
        }
        let url


        // 缺书网
        // http://www.queshu.com/search/?c=9787502646967
        res = await superagent.get(`http://www.queshu.com/search/?c=${isbn}`).catch(() => {})
        if (res && res.text) {
            $ = cheerio.load(res.text);
            if ($('#jianlou_book').length > 0) {
                let text = $('#jianlou_book .book_right_line').eq(2).text()
                text = text.split(' ')
                text[0] = text[0].replace(/\s*/g, '')
                author.name = text[0].split('，')
                resolve(author)
                return
            }
        }

        // 孔夫子旧书网
        url = `http://search.kongfz.com/product_result/?key=${isbn}&status=0&_stpmt=eyJzZWFyY2hfdHlwZSI6ImFjdGl2ZSJ9&pagenum=1&ajaxdata=1&type=1&_=${dayjs().valueOf()}`
        res = await axios.get(url).catch(() => {})
        if (res.data && res.data.data && res.data.data.itemList && res.data.data.itemList.length > 0) {
            const book = res.data.data.itemList[0]
            const nameString = book.author.replace('著', '').replace('编', '').trim()
            author.name = nameString.split('、');
            resolve(author)
        }


        resolve(author)
    })
}

function exportExcel() {
    const data = [
        ['isbn', '书名', '封面', '作者', '作者简介', '书本简介', '出版社', '分类', '页数', '价格', '装帧', '出版时间']
    ];
    for (let i = 0; i < books.length; i++) {
        data.push([
            books[i].isbn,
            books[i].title,
            books[i].images,
            books[i].author.name.join(','),
            books[i].author.intro,
            books[i].summary,
            books[i].publisher,
            books[i].kind,
            books[i].pages,
            books[i].price,
            books[i].binding,
            books[i].pubdate,
        ])

    }
    const name = dayjs().format('YYYYMMDDHHmmss')
    var buffer = xlsx.build([{
        name: name,
        data: data
    }]); // Returns a buffer
    fs.writeFile(`export/${name}.xlsx`, buffer, function (err) {
        if (err) {
            console.log("Write failed: " + err);
            return;
        }
    });
}

function exportJson() {
    const str = JSON.stringify(books)
    const name = dayjs().format('YYYYMMDDHHmmss')
    fs.writeFile(`export/${name}.json`, str, function (err) {
        if (err) {
            console.log("Write failed: " + err);
            return;
        }
    });
}